﻿using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 生产废料控制器
    /// </summary>
    [Route("[controller]/[action]")]
    public class ProductionWasteOrderController : Controller
    {
        /// <summary>
        /// 生产废料服务接口
        /// </summary>
        private readonly IProductionWasteOrderService _productionWasteOrderService;
        private readonly IProductionWorkOrderMainServices _productionWorkOrderMainServices;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionWasteOrderService">生产废料服务接口</param>
        public ProductionWasteOrderController(IProductionWasteOrderService productionWasteOrderService, IProductionWorkOrderMainServices productionWorkOrderMainServices)
        {
            _productionWasteOrderService = productionWasteOrderService;
            this._productionWorkOrderMainServices = productionWorkOrderMainServices;
        }

        /// <summary>
        /// 生产废料视图
        /// </summary>
        /// <returns>返回 `ProductionWasteOrderIndex.cshtml` 视图页面</returns>
        [HttpGet]
        public IActionResult ProductionWasteOrderIndex()
        {
            return View();
        }
        
        /// <summary>
        /// 获取生产废料单列表 (API)
        /// </summary>
        /// <param name="input">包含分页和筛选参数的输入DTO</param>
        /// <returns>返回符合Layui表格渲染要求的JSON数据</returns>
        [HttpGet]
        public async Task<IActionResult> GetList(GetProductionWasteOrderInputDto input)
        {
            var result = await _productionWasteOrderService.GetListAsync(input);
            // 将结果包装成Layui表格约定的数据格式（code, msg, count, data）
            return Json(new { code = 0, msg = "success", count = result.TotalCount, data = result.Items });
        }

        /// <summary>
        /// 创建生产废料单 (API)
        /// </summary>
        /// <param name="input">从请求体（Request Body）中绑定的废料单数据</param>
        /// <returns>返回操作成功的JSON消息</returns>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CreateUpdateProductionWasteOrderDto input)
        {
            await _productionWasteOrderService.CreateAsync(input);
            return Json(new { success = true, message = "创建成功" });
        }

        /// <summary>
        /// 更新生产废料单 (API)
        /// </summary>
        /// <param name="id">要更新的废料单ID (从URL中获取)</param>
        /// <param name="input">从请求体（Request Body）中绑定的废料单新数据</param>
        /// <returns>返回操作成功的JSON消息</returns>
        [HttpPost]
        public async Task<IActionResult> Update(int id, [FromBody] CreateUpdateProductionWasteOrderDto input)
        {
            await _productionWasteOrderService.UpdateAsync(id, input);
            return Json(new { success = true, message = "更新成功" });
        }

        /// <summary>
        /// 删除生产废料单 (API)
        /// </summary>
        /// <param name="id">要删除的废料单ID (从URL中获取)</param>
        /// <returns>返回操作成功的JSON消息</returns>
        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            await _productionWasteOrderService.DeleteAsync(id);
            return Json(new { success = true, message = "删除成功" });
        }



        // ------------------------------------
        /// <summary>
        /// 查询生产工单
        /// </summary>
        /// <returns>返回所有生产工单的数据列表</returns>
        [HttpGet]
        public async Task<IActionResult> QueryWorkOrder()
        {
            var info = await _productionWorkOrderMainServices.QueryWorkOrder();
            return Json(info);
        }
    }
}
